package com.ql.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ql.dao.DeviceMonitoringDao;
import com.ql.dao.IUserDao;
import com.ql.dao.SectionDao;
import com.ql.domain.Device;
import com.ql.domain.DeviceMonitoring;
import com.ql.domain.Section;
import com.ql.domain.request.DeviceMonitoringQuery;
import com.ql.domain.response.JsonResult;
import com.ql.service.DeviceMonitoringService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DeviceMonitoringServiceImpl implements DeviceMonitoringService {
    @Autowired
    DeviceMonitoringDao deviceMonitoringDao;
    @Autowired
    SectionDao sectionDao;

    @Override
    public JsonResult findAll(DeviceMonitoringQuery deviceMonitoringQuery, Integer page, Integer limit) {
        //进行分页
        List<DeviceMonitoring> deviceMonitoringList = new ArrayList<>();
        //如果userId不为空并且projectId和sectionId为空
        if (StringUtils.isNotEmpty(deviceMonitoringQuery.getUserId()) && StringUtils.isEmpty(deviceMonitoringQuery.getProjectId()) && StringUtils.isEmpty(deviceMonitoringQuery.getSectionId())) {
            //根据userId查询出对应的设备id
            List<Section> sectionList = sectionDao.findDeviceByUserId(deviceMonitoringQuery.getUserId());
            PageHelper.startPage(page, limit);
            deviceMonitoringList = disposeList(sectionList, deviceMonitoringQuery);
        }
        //如果projectId不为空,sectionId为空
        else if (StringUtils.isNotEmpty(deviceMonitoringQuery.getProjectId()) && StringUtils.isEmpty(deviceMonitoringQuery.getSectionId())) {
            //根据projectId查询对应的设备id
            List<Section> sectionList = sectionDao.findByProjectId(deviceMonitoringQuery.getProjectId());
            PageHelper.startPage(page, limit);
            deviceMonitoringList = disposeList(sectionList, deviceMonitoringQuery);
        } else if (StringUtils.isNotEmpty(deviceMonitoringQuery.getSectionId())) {
            List<Section> sectionList = sectionDao.findBySectionId(deviceMonitoringQuery.getSectionId());
            PageHelper.startPage(page, limit);
            deviceMonitoringList = disposeList(sectionList, deviceMonitoringQuery);
        }

        PageInfo pageInfo = new PageInfo(deviceMonitoringList);
        return JsonResult.success(pageInfo);
    }

    private List<DeviceMonitoring> disposeList(List<Section> sectionList, DeviceMonitoringQuery deviceMonitoringQuery) {
        //保存设备id
        ArrayList<Integer> deviceIds = new ArrayList<>();
        if (sectionList != null) {
            for (Section section : sectionList) {
                List<Device> devices = section.getDevices();
                    for (Device device : devices) {
                        //获得用户关联的所有设备id
                        deviceIds.add(device.getId());
                    }
            }
            if (deviceIds.size()==0){
                return new ArrayList<DeviceMonitoring>();
            }
            //处理查询结果,将进度和当前作业查询出并赋值
            return handResult(deviceMonitoringDao.findAllByUserId(deviceIds, deviceMonitoringQuery));

        }
        return new ArrayList<DeviceMonitoring>();
    }

    private List<DeviceMonitoring> handResult(List<DeviceMonitoring> deviceMonitorings) {
        for (DeviceMonitoring deviceMonitoring : deviceMonitorings) {
            //获取设备编号
            String devNum = deviceMonitoring.getDevNum();
            //根据设备编号查询出设备总桩孔数
            int devTotal = deviceMonitoringDao.findDevTotalByDevNum(devNum);
            //查询出已完成的桩孔数
            int finishDevTotal = deviceMonitoringDao.findFinishDevTotal(devNum);
            //完成百分比
            DecimalFormat df = new DecimalFormat("0");//格式化小数
            String schedule = df.format(((float) finishDevTotal / devTotal) * 100);
            deviceMonitoring.setSchedule(schedule);
            //获得当前作业桩号
            String stakeMark = deviceMonitoringDao.findWorkStakeMark(devNum);
            deviceMonitoring.setPileMark(stakeMark);
        }
        return deviceMonitorings;
    }
}
